package com.nega.modules.handler;

import com.nega.commons.model.SearchResult;
import com.nega.commons.spider.IPSourcePool;
import com.nega.commons.spider.SpiderCoreService;
import com.nega.modules.service.SpiderService;
import com.nega.modules.thread.SpiderThreadSouGou;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Author: Garcia
 * @CreateDate: 2019/5/8 14:56
 * @Description: 搜狗扫描
 */
@Service
@Slf4j
public class SpiderSouGouServiceImpl implements SpiderService {

    @Resource
    private SpiderCoreService spiderCoreService;

    @Resource
    private IPSourcePool ipSourcePool;


    @Override
    public List<SearchResult> getResultInfo(String text) {

        log.info("start 搜狗数据爬取>>>");
        ExecutorService executorService = Executors.newFixedThreadPool(7);
        List<Future<List<SearchResult>>> futures = new ArrayList<>();

        for (int i = 1; i <= 7; i++) {
            Future<List<SearchResult>> future = executorService.submit(new SpiderThreadSouGou(spiderCoreService, ipSourcePool, text, (i)));
            futures.add(future);
        }

        log.info("搜狗数据提取>>>>>>>>>>>>");
        List<SearchResult> results = new ArrayList<>();
        long start = System.currentTimeMillis();

        for (Future<List<SearchResult>> future : futures) {
            try {
                List<SearchResult> resu = future.get();
                if (resu.size() < 1) {
                    continue;
                }
                results.addAll(resu);
            } catch (Exception e) {
                log.error("搜狗读取异常", e);
            } finally {
                executorService.shutdown();
            }
        }
        log.info("数据提取结束，搜狗数量:{}，搜狗耗时:{}",results.size(), (System.currentTimeMillis() - start));
        return results;
    }

}
